package com.qfedu.openplateform.gateway.filter;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 时间拦截
 * 1.获取时间 time
 * 2.将 time 和当前时间  比较 如果时间 大于30s  拒绝
 *                                   小于30s  放行
 */

public class TimerFilter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //请求参数里获得时间
        String paramTime = request.getParameter("time");
        //getTime()获得时间戳
        Long timeCha = new Date().getTime() - new Date(paramTime).getTime();
        //时间格式类
        String timeGap = new SimpleDateFormat("ss").format(timeCha);
        System.out.println("timeGap = " + timeGap);
        //进行判断
        if (Integer.valueOf(timeGap) <= 30) {
            return true;
        }
        response.getWriter().println("time error");
        return false;
    }
}

